/** File: 	MainWindow.java
 *  Author:	Christopher Hayes
 *	Date:	Oct 15, 2009
 * 	Class:  CMSC 345
 * 
 */
package cx3k.GUI;

import java.awt.*;

import javax.swing.*;

import cx3k.dataaccess.search.Searcher;
import cx3k.scheduler.Scheduler;
import cx3k.scheduler.objects.Course;

/**
 * @author chris
 *
 */
public class MainWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Color to be used throughout the program to display components
	 */
	//public static final Color MAINCOLOR = new Color(251, 210, 0);
	public static final Color MAINCOLOR = Color.LIGHT_GRAY;
	
	private ProfilePane profile;
	private SchedulePane schedule;
	private ClassInfo classInfo;
	private SearchPane search;
	private Scheduler scheduler;
	private Searcher searcher;

	/**
	 * 
	 * @param lf LoadFrame
	 * @param scheduler Scheduler
	 * @param searcher Searcher 
	 */
	public MainWindow (LoadFrame lf, Scheduler scheduler, Searcher searcher) {
		
		
		this.setTitle("Cx3K Scheduler");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(1000, 700);
		this.setResizable(false);
		GridBagLayout gbl = new GridBagLayout();
		this.setLayout(gbl);
		GridBagConstraints gbc = new GridBagConstraints();
		
		
		
		this.scheduler = scheduler;
		this.scheduler.newProfile("Your name here.");
		this.searcher = searcher;
		
		lf.setValue(75);
		lf.addText("Creating Workspace...\n");
		
		lf.addText("\tClassInfo...");
		classInfo = new ClassInfo();
		lf.addText("Done\n");
		
		lf.addText("\tProfilePane...");
		profile = new ProfilePane(scheduler, classInfo);
		lf.addText("Done\n");
		
		profile.updateProfile();
		CompletedClasses cc = profile.getCompletedClasses();
		
		lf.addText("\tSchedulePane...");
		schedule = new SchedulePane(scheduler, classInfo, cc);
		lf.addText("Done\n");
		
		lf.addText("\tSearchPane...");
		search = new SearchPane(classInfo, searcher, cc, schedule);
		lf.addText("Done\n");
		
		lf.addText("\tMenuBar...");
		this.setJMenuBar(new MyMenuBar(scheduler, schedule, profile));
		
		lf.addText("Done\n");
		lf.setValue(85);
		lf.addText("Laying out Components...");
		
		//Create Profile and add it to frame
		
		gbc.gridx = 0;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 45; 
		gbc.weighty = 50; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(profile, gbc);
		this.add(profile); 
		
		//Create Schedule and add it to frame
		
		gbc.gridx = 0;
		gbc.gridy = 1; 
		gbc.gridwidth = 3; 
		gbc.gridheight = 1; 
		gbc.weightx = 85; 
		gbc.weighty = 50; 
		gbl.setConstraints(schedule, gbc);
		this.add(schedule); 
		
		lf.setValue(90);
		
		//Create ClassInfo and add it to frame
		
		gbc.gridx = 2;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 40; 
		gbc.weighty = 60; 
		gbl.setConstraints(classInfo, gbc);
		this.add(classInfo); 
		
		//Create Search and add it to frame
		
		gbc.gridx = 3;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 2; 
		gbc.weightx = 15; 
		gbc.weighty = 100; 
		gbl.setConstraints(search, gbc);
		this.add(search); 
		
		lf.setValue(95);
		lf.addText("Done\n");
		
		//Center frame on screen
		Toolkit toolkit = this.getToolkit();
	    Dimension size = toolkit.getScreenSize();
	    this.setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
	    
	    
		this.setVisible(true);
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LoadFrame lf = new LoadFrame();
		
		Scheduler sc = null;
		Searcher se = null;
		try {
			lf.addText("Loading Searcher...");
			se = new Searcher(Course.class);
			lf.setValue(25);
			lf.addText("Done\n");
			
			lf.addText("Loading Scheduler...");
			sc = new Scheduler();
			lf.setValue(50);
			lf.addText("Done\n");
		}
		catch (Exception e){
			ConnectFrame cf = new ConnectFrame(sc, se);
			System.out.println("Waiting");
			Object[] o = cf.waitForConnect();
			cf.setVisible(false);
			sc = (Scheduler) o[0];
			se = (Searcher) o[1];
		}
		MainWindow m = new MainWindow(lf, sc, se);
		lf.setValue(100);
		lf.setVisible(false);
		m.toString();
	}

}
